package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.AdvancesOnDoInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/3/5.
 * 特别任务已撤销
 */

public class TbOnDoFragment extends BaseStoreHouseRefreshRecyclerFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    private List<AdvancesOnDoInfo> infoList;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new AdvancesOnDoAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;

    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        AdvancesOnDoInfo info = (AdvancesOnDoInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
        args.putSerializable("info", info);
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.getCancelOrderList(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),"001",
                                         "7",
                                         new JsonCallback<AdvancesOnDoInfo>(
                                                 AdvancesOnDoInfo.class) {

                                             @Override
                                             public void onError(Call call, Exception e) {
                                                 onRequestError();
                                             }

                                             @Override
                                             public void onResponse(
                                                     ResponseInfo<AdvancesOnDoInfo> response
                                             ) {
                                                 int code = response.getCode();
                                                 if (code == 1) {
                                                     mErrorLayout.setErrorType(
                                                             EmptyLayout.NETWORK_ERROR);
                                                     return;
                                                 }
                                                 infoList = response.getData();
                                                 if (infoList!=null) {
                                                     Collections.sort(infoList,
                                                                      new Comparator<AdvancesOnDoInfo>() {
                                                                          @Override
                                                                          public int compare(
                                                                                  AdvancesOnDoInfo advancesOnDoInfo,
                                                                                  AdvancesOnDoInfo t1
                                                                          ) {
                                                                              return (t1
                                                                                      .getAddtime())
                                                                                      .compareTo(
                                                                                              (advancesOnDoInfo.getAddtime()));
                                                                          }
                                                                      }
                                                     );
                                                 }
                                                 onRequestResponse(infoList);
                                             }

                                             @Override
                                             public void onAfter() {
                                                 super.onAfter();
                                                 onRequestFinish();
                                             }
                                         }
        );
    }

    public static class AdvancesOnDoAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(
                    R.layout.item_advances_ondo_fragment, parent, false);
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(
                View view, int viewType
        ) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            AdvancesOnDoInfo info = (AdvancesOnDoInfo) _data.get(position);
            vh.tv_shopName.setText(info.getAccount());
            vh.tv_orderId.setText("订单编号:" + info.getOrderid());
            vh.tv_time.setText("撤销时间:" + info.getAddtime());
            vh.tv_cause.setText("撤销原因:" + info.getReason());
        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

            @BindView(R.id.tv_orderId)
            TextView tv_orderId;
            @BindView(R.id.tv_time)
            TextView tv_time;
            @BindView(R.id.tv_cause)
            TextView tv_cause;
            @BindView(R.id.tv_shopName)
            TextView tv_shopName;

            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }

    }

}
